package com.kkwrite.regimen.consumer.controller.address;

import com.kkwrite.regimen.common.entity.dto.address.RegPositionLogInDTO;
import com.kkwrite.regimen.common.entity.dto.address.RegPositionLogOutDTO;
import com.kkwrite.regimen.consumer.service.address.PositionService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by lisha on 2018/7/12 22:24.
 *
 * @author lisha
 */
@RestController
@RequestMapping(value = "/position")
@Api(value = "定位信息模块")
public class PositionController {

    @Resource(name = "positionService")
    private PositionService positionService;

    @PostMapping(value = "/update")
    @HystrixCommand(fallbackMethod = "saveOrUpdateError")
    @ApiOperation(value = "提交用户定位信息")
    public RegPositionLogOutDTO saveOrUpdate(@RequestBody RegPositionLogInDTO inDTO, HttpServletRequest request) {
        // 入参校验
        if (StringUtils.isBlank(inDTO.getAddress()) || inDTO.getLatitude() == null
                || inDTO.getLongitude() == null) {
            return new RegPositionLogOutDTO("false", "入参不合法！");
        }
        return positionService.saveOrUpdate(inDTO, request.getHeader("token"));
    }

    public RegPositionLogOutDTO saveOrUpdateError(RegPositionLogInDTO inDTO, HttpServletRequest request) {
        RegPositionLogOutDTO out = new RegPositionLogOutDTO();
        out.setSuccess("false");
        out.setMsg("服务异常！");
        return out;
    }

}
